/*
 * Copyright (C) 2008 Alessandro Sivieri <alessandro.sivieri@chimera-bellerofonte.eu>.
 *
 *  This file is part of Daex.
 *
 *  Daex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.chimera_bellerofonte.tesi.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import eu.chimera_bellerofonte.tesi.DaexIcon;


/**
 * Classe per il dialogo dell'about.
 * 
 * @author Sivieri Alessandro
 * @author Vitucci Nicola
 * @version 17/mag/07
 */
public class About extends JPanel
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;

	/**
	 * @param owner
	 */
	private About()
	{
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(362, 203);
		this.add(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jLabel = new JLabel();
			jLabel.setIcon(new ImageIcon(getClass().getResource(DaexIcon.LOGO)));
			jLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
			jLabel.addMouseListener(new MouseAdapter()
			{

				/**
				 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
				 */
				@Override
				public void mouseClicked(MouseEvent e)
				{
					getJContentPane().remove(getJPanel());
					getJContentPane().add(new EasterEggPane());
					About.this.updateUI();
				}
				
			});
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
			jContentPane.add(jLabel, BorderLayout.WEST);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel()
	{
		if (jPanel == null)
		{
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.weightx = 1.0D;
			gridBagConstraints1.weighty = 0.4D;
			gridBagConstraints1.gridy = 1;
			jLabel2 = new JLabel();
			jLabel2.setText("<html>Creato/scritto ed interpretato da<br>Sivieri Alessandro<br>Vitucci Nicola</html>");
			jLabel2.setFont(new Font("Dialog", Font.ITALIC, 14));
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.weightx = 1.0D;
			gridBagConstraints.weighty = 0.6D;
			gridBagConstraints.gridy = 0;
			jLabel1 = new JLabel();
			jLabel1.setText("DVD Audio EXtractor");
			jLabel1.setFont(new Font("Dialog", Font.BOLD, 24));
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(jLabel1, gridBagConstraints);
			jPanel.add(jLabel2, gridBagConstraints1);
		}
		return jPanel;
	}
	
	public static void showAbout(Component c)
	{
		About about=new About();
		JOptionPane pane=new JOptionPane();
		pane.setIcon(new ImageIcon());
		pane.setMessage(about);
		JDialog dialog=pane.createDialog(c, "About Daex...");
		dialog.setModal(true);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
	}
	
	private static class EasterEggPane extends JPanel
	{
		private static final long serialVersionUID = 1L;
		private JTextField jTextField = null;
		private JButton jButton = null;
		private JLabel jLabel = null;
		private HashMap<Character, Integer> roman=null;

		private EasterEggPane()
		{
			super();
			roman=new HashMap<Character, Integer>();
			roman.put('I', 1);
			roman.put('V', 5);
			roman.put('X', 10);
			roman.put('L', 50);
			roman.put('C', 100);
			roman.put('D', 500);
			roman.put('M', 1000);
			roman.put('i', 1);
			roman.put('v', 5);
			roman.put('x', 10);
			roman.put('l', 50);
			roman.put('c', 100);
			roman.put('d', 500);
			roman.put('m', 1000);
			initialize();
		}
		
		/**
		 * This method initializes this
		 * 
		 * @return void
		 */
		private void initialize()
		{
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.weightx = 1.0D;
			gridBagConstraints2.weighty = 0.4D;
			gridBagConstraints2.insets = new Insets(0, 5, 0, 5);
			gridBagConstraints2.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("<html>Easter Egg!!!<br>Hai di fronte un convertitore<br>di numeri romani!</html>");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.weightx = 1.0D;
			gridBagConstraints1.weighty = 0.3D;
			gridBagConstraints1.gridy = 2;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 0.3D;
			gridBagConstraints.insets = new Insets(0, 5, 0, 5);
			gridBagConstraints.gridx = 0;
			this.setSize(232, 160);
			this.setLayout(new GridBagLayout());
			this.add(getJTextField(), gridBagConstraints);
			this.add(getJButton(), gridBagConstraints1);
			this.add(jLabel, gridBagConstraints2);
		}

		/**
		 * This method initializes jTextField	
		 * 	
		 * @return javax.swing.JTextField	
		 */
		private JTextField getJTextField()
		{
			if (jTextField == null)
			{
				jTextField = new JTextField();
				jTextField.setText("Inserisci un numero...");
				jTextField.addFocusListener(new FocusAdapter()
				{

					/**
					 * @see java.awt.event.FocusAdapter#focusGained(java.awt.event.FocusEvent)
					 */
					@Override
					public void focusGained(FocusEvent e)
					{
						((JTextField) e.getComponent()).selectAll();
					}

					/**
					 * @see java.awt.event.FocusAdapter#focusLost(java.awt.event.FocusEvent)
					 */
					@Override
					public void focusLost(FocusEvent e)
					{
						((JTextField) e.getComponent()).select(0, 0);
					}
					
				});
			}
			return jTextField;
		}

		/**
		 * This method initializes jButton	
		 * 	
		 * @return javax.swing.JButton	
		 */
		private JButton getJButton()
		{
			if (jButton == null)
			{
				jButton = new JButton();
				jButton.setText("Converti!");
				jButton.setMnemonic(KeyEvent.VK_C);
				jButton.setToolTipText("Converti il numero da romano a decimale");
				jButton.addActionListener(new ActionListener()
				{

					public void actionPerformed(ActionEvent e)
					{
						getJTextField().setText(getTotal(getJTextField().getText()));
					}
					
				});
			}
			return jButton;
		}
		
		private String getTotal(String number)
		{
			int total=0, prec, act, temp;
			boolean hop=true, next;
			ArrayList<Integer> cn=new ArrayList<Integer>();
			ArrayList<Integer> new_cn=new ArrayList<Integer>();
			
			for(char c: number.toCharArray())
			{
				if(roman.containsKey(c))
				{
					cn.add(roman.get(c));
				}
				else
				{
					return "La stringa deve contenere solo I/V/X/L/C/D/M";
				}
			}
			while(cn.size()>1 && hop)
			{
				prec=0;
				act=0;
				hop=false;
				next=true;
				new_cn.clear();
				for(int i=0; i<cn.size(); i++)
				{
					act=cn.get(i);
					if(act>prec && i!=0 && next)
					{
						hop=true;
						next=false;
						temp=act-prec;
						new_cn.remove(new_cn.size()-1);
						new_cn.add(temp);
					}
					else
					{
						new_cn.add(act);
						if(act<=prec && !next)
						{
							next=true;
						}
					}
					prec=act;
				}
				if(new_cn.size()>0)
				{
					cn.clear();
					cn.addAll(new_cn);
				}
			}
			for(int i: cn)
				total+=i;
			return ""+total;
		}
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
